//
//  SettingViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/6.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "SettingV2ViewController.h"

#import "EditUserInfoV2ViewController.h"
#import "ResetPwdViewController.h"

@interface SettingV2ViewController ()

/// 模型视图
@property (nonatomic, readonly, strong) SettingV2ViewModel* viewModel;

// UI

//IBOutlet
@property (weak, nonatomic) IBOutlet UIButton* editBtn;
@property (weak, nonatomic) IBOutlet UIButton* pwdBtn;
@property (weak, nonatomic) IBOutlet UISwitch* msgSwitchBtn;
@property (weak, nonatomic) IBOutlet UILabel* versionLabel;
@property (weak, nonatomic) IBOutlet UIButton* logoutBtn;

@end

@implementation SettingV2ViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData
{
    
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
}

- (void)initUI
{
    @weakify(self);
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [willAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [didAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        // ScrollView 内容 未能在此时才约束
        
        //Request
    }];
    
    [[self rac_signalForSelector:@selector(viewWillDisappear:)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
    
    [[[viewDidLayoutSVSignal take:1] deliverOnMainThread] subscribeNext:^(id x)
     {
         @strongify(self)
         
         //重置 更新 frame
         [self _setupAllView];
     }];
    
#pragma mark -- bindViewModel
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    [[self.editBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        @strongify(self);
        [self _push2EditUserInfoVC];
    }];
    
    [[self.pwdBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        @strongify(self);
        [self _push2ResetPwdVC];
    }];
    
    self.logoutBtn.rac_command = self.viewModel.logoutCmd;
    [[self.viewModel.logoutCmd.executionSignals.switchToLatest deliverOnMainThread] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [[NSNotificationCenter defaultCenter] postNotificationName:Notify_Tabbar_Select object:@0];
    }];
}

#pragma mark - 更新初始化 操作
- (void)_setupAllView
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
}

#pragma mark - Delegate 一些无法用RAC实现
// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}

#pragma mark-代理方法
// 设立实现代理，注意要返回UIModalPresentationNone
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

#pragma mark - 功能

#pragma mark - 跳转
- (void)_push2EditUserInfoVC
{
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    UserDetailData* userDetailData = um.userDetailData;
    NSInteger identify = [userDetailData.user.identify integerValue];
    NSInteger status = 0;
    NSInteger finalIdentify = 1;
    if (identify > 1)
    {
        status = [userDetailData.content.status integerValue];
        
        if (2 == status)
        {
            finalIdentify = identify;
        }
    }
    else
    {
        status = [userDetailData.user.status integerValue];
    }
    
    EditUserInfoV2ViewModel* vm = [[EditUserInfoV2ViewModel alloc] initWithParams:@{SUViewModelTitleKey:@"修改资料"}];
    vm.type = finalIdentify-1;
    EditUserInfoV2ViewController* vc = [[EditUserInfoV2ViewController alloc] initWithViewModel:vm];
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)_push2ResetPwdVC
{
    ResetPwdViewModel* vm = [[ResetPwdViewModel alloc] initWithParams:@{SUViewModelTitleKey:@"修改密码"}];
    ResetPwdViewController* vc = [[ResetPwdViewController alloc] initWithViewModel:vm];
    
    [self.navigationController pushViewController:vc animated:YES];
}

@end
